package com.xindacloud.sms.portal.handler;

import com.xindacloud.sms.portal.exception.RestTemplateException;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.web.reactive.function.client.WebClientResponseException;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Data
/*@NoArgsConstructor*/
public class ErrorResponse {


    private String timestamp;

    /**
     * HTTP Status Code
     */
    private int status;

    /**
     * HTTP Reason phrase
     */
    private String error;

    /**
     * A message that describe the error thrown when calling the downstream API
     */
    private String message;

    /**
     * Downstream API name that has been called by this application
     */
    private String api;

    /**
     * URI that has been called
     */
    private String path;

    public ErrorResponse(RestTemplateException ex, String path) {
        this.timestamp = DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(LocalDateTime.now());
        this.status = ex.getStatusCode().value();
        this.error = ex.getStatusCode().getReasonPhrase();
        this.message = ex.getError();
        this.api = ex.getApi();
        this.path = path;
    }

    public ErrorResponse(WebClientResponseException ex, String path) {
        this.timestamp = DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(LocalDateTime.now());
        this.status = ex.getStatusCode().value();
        this.error = ex.getStatusCode().getReasonPhrase();
        this.message = ex.getResponseBodyAsString();
        this.api = ex.getRequest().getURI().getPath();
        this.path = path;
    }
}